var drawGraphic;
var createDrawToolbar;
var initDrawData;
var drawGraphicsLayer
var drawDataToolbar;
require([
    "esri/layers/GraphicsLayer",
    "dojo/on", "dojo/dom",

    "esri/symbols/SimpleMarkerSymbol",
    "esri/symbols/SimpleLineSymbol",
    "esri/symbols/SimpleFillSymbol",
    "esri/symbols/PictureMarkerSymbol",

    "esri/toolbars/draw",
    "esri/graphic",


    "dojo/domReady!"
], function (
    GraphicsLayer,
    on, dom,
    SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol, PictureMarkerSymbol,
    Draw, Graphic
) {
    initDrawData = function () {
        drawGraphicsLayer = new GraphicsLayer();
        map.addLayer(drawGraphicsLayer);
        createDrawToolbar();
    }

    createDrawToolbar = function () {
        drawDataToolbar = new Draw(map, {
            showTooltips: true
        });

        drawDataToolbar.on("draw-complete", addToMap);
    }

    var drawCollection = ["polygon", "freehand_polygon", "circle"];

    //为每个绘图按钮绑定click事件
    var list_obj = document.getElementsByTagName('button');

    for (var i = 0; i < list_obj.length; i++) {
        for (var j = 0; j < drawCollection.length; j++) {
            var buttonId = "drawdata_" + drawCollection[j];
            var currentId = list_obj[i].id;
            if (currentId == buttonId) {
                (function (i, currentId) {
                    on(dom.byId(currentId), "click", function activateTool() {
                        var tool = currentId.toUpperCase().substring(9);
                        // drawGraphicsLayer.clear();

                        drawDataToolbar.activate(Draw[tool]);
                    });
                })(i, currentId);
            }
        }
    }

    on(dom.byId("clear_draw1"), "click", function () {
        drawGraphicsLayer.clear();
        map.infoWindow.hide();
        drawDataToolbar.deactivate();
    });

    on(dom.byId("stop_draw1"), "click", function () {
        drawDataToolbar.deactivate();
    });

    function addToMap(evt) {
        var symbol;
        drawGraphic = null;
        // drawDataToolbar.deactivate();
        switch (evt.geometry.type) {
            case "point":
            case "multipoint":
                symbol = new SimpleMarkerSymbol({
                    "color": [238, 69, 0, 64],
                    "size": 6,
                    "angle": 0,
                    "xoffset": 0,
                    "yoffset": 0,
                    "type": "esriSMS",
                    "style": "esriSMSCircle",
                    "outline": {
                        "color": [238, 69, 0, 160],
                        "width": 1,
                        "type": "esriSLS",
                        "style": "esriSLSSolid"
                    }
                });
                break;
            case "polyline":
                symbol = new SimpleLineSymbol({
                    "color": [255, 60, 0],
                    "width": 1,
                    "type": "esriSLS",
                    "style": "esriSLSSolid"
                });
                break;
            default:
                symbol = highlightPolySymbol;
                break;
        }
        drawGraphic = new Graphic(evt.geometry, symbol, {}, {});
        drawGraphicsLayer.add(drawGraphic);
    }

    on(dom.byId("savaDrawDataBtn"), "click", function () {
        if (drawGraphicsLayer.graphics.length > 0) {
            drawDataToolbar.deactivate();
            var name = $("#drawDataNameInput").val() || `自定义数据`
            createFeatureLayer(drawGraphicsLayer.graphics, name, "drawLayerList").then(() => {
                setTab('mainio', 3, 4)
                drawGraphicsLayer.clear();
            });
        } else {
            warningAlert("请先进行绘制再进行保存！")
        }
    })

    $("#locationSearch").click(function () {
        $("#addressPanel input").val("");
        $("#addressPage").empty();
        $("#addressList>ul").empty();
        $("#addressPanel").show();
        drawDataToolbar.deactivate();

        $("#addressPanel .close").hide();
        $("#addressPanel .back").show();
    });

    $("#addressPanel .back").click(function () {
        $("#addressPanel input").val("");
        $("#addressPage").empty();
        $("#addressList>ul").empty();
        $("#addressPanel").hide();
        thematicGraphicLayer.clear();

        $("#addressPanel .close").show();
        $("#addressPanel .back").hide();
    });

})